package org.ddd.orderdemo.application.event.subscribe;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.ddd.orderdemo.application.event.DomainEvent;
import org.ddd.orderdemo.application.event.Event;
import org.ddd.orderdemo.application.event.center.EventBusExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Demo1Subscriber implements Subscriber,AutoCloseable {

    private Logger logger = LoggerFactory.getLogger(EventBusExceptionHandler.class);

    private EventBus eventBus;

    public Demo1Subscriber(EventBus eventBus) {
        this.eventBus = eventBus;
        eventBus.register(this);
    }


    /**
     * 订阅方法必须有且仅有一个参数
     * 必须使用@Subscribe注解
     * 方法需为public
     * @param event
     */
    @Subscribe // 处理方法
    public void onEvent(Event event) {

        // 复杂业务逻辑
        logger.info("event:{}", event);

    }


    @Subscribe
    public void updateSharedState(Event event) {
        // 未同步的共享资源访问
//        counter++;
    }


    /**
     * 订阅方法必须有且仅有一个参数
     * 必须使用@Subscribe注解
     * 方法需为public
     * @param event
     */
    @Subscribe // 处理方法
    public void notify(DomainEvent event) {

        // 复杂业务逻辑
        logger.info("event:{}", event);

    }


    @Override
    public void close() throws Exception {
        eventBus.unregister(this);
    }
}
